home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / write.c < prev    next >
C/C++ Source or Header  |  1993-10-20  |  2KB  |  95 lines

  1. /*
  2.  * _write: like write, but takes a long instead of an int. Written by
  3.  * Eric R. Smith and placed in the public domain.
  4.  */
  5.  
  6. /* BUG: under TOS, CRMOD doesn't work unless RAW is on or SIGINT is
  7.  * being caught
  8.  */
  9.  
  10. #include <osbind.h>
  11. #include <fcntl.h>
  12. #include <ioctl.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <stat.h>
  17. #include <mintbind.h>
  18. #include "lib.h"
  19.  
  20. extern __Sigfunc _sig_handler[]; /* TOS fakes for signal handling */
  21.  
  22. long
  23. _write(fd, buf, size)
  24.     int fd;
  25.     const void *buf;
  26.     unsigned long size;
  27. {
  28.     unsigned char c, *foo;
  29.     unsigned flags;
  30.     long r;
  31.     extern int __mint;
  32.     struct stat statbuf;
  33.     _DOSTIME timebuf;
  34.  
  35.     if (__mint == 0 && isatty(fd)) {
  36.         r = __OPEN_INDEX(fd);
  37.         if (r < 0 || r >= __NHANDLES)
  38.             r = __NHANDLES - 1;
  39.         flags = __open_stat[r].flags;
  40.         if ((flags & RAW) || _sig_handler[SIGINT] != SIG_DFL) {
  41.             foo = (unsigned char *) buf;
  42.             r = size;
  43.             while (r-- > 0) {
  44.                 c = *foo++;
  45.                 if (c == '\n' && (flags & CRMOD))
  46.                     _console_write_byte(fd, '\r');
  47.                 _console_write_byte(fd, c);
  48.             }
  49.             return size;
  50.         }
  51.     }
  52.  
  53.     r = Fwrite(fd, size, buf);
  54.     if (r < 0) {
  55.         errno = (int) -r;
  56.         return -1;
  57.     }
  58.     if (size && r == 0) {
  59.       if (__mint >= 9)
  60.         {
  61.           if ((r = Fcntl (fd, &statbuf, FSTAT)) < 0)
  62.         {
  63.           errno = (int) -r;
  64.           return -1;
  65.         }
  66.           if ((statbuf.st_mode & S_IFMT) == S_IFREG)
  67.         {
  68.           errno = ENOSPC;
  69.           return -1;
  70.         }
  71.         }
  72.       else if (Fdatime (&timebuf, fd, 0) == 0
  73.            && Fseek (0L, fd, SEEK_CUR) >= 0)
  74.         {
  75.           errno = ENOSPC;
  76.           return -1;
  77.         }
  78.     }
  79.  
  80.     return r;
  81. }
  82.  
  83. #if defined (__GNUC__) && !defined (__MSHORT__)
  84. __asm__ (".stabs \"_write\",5,0,0,__write");
  85. #else
  86. int
  87. write(fd, buf, size)
  88.     int fd;
  89.     const void *buf;
  90.     unsigned size;
  91. {
  92.     return (int) _write(fd, buf, (unsigned long)size);
  93. }
  94. #endif
  95.